1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 25 module gobject.WeakRef; 26 27 private import gobject.ObjectG; 28 private import gobject.c.functions; 29 public import gobject.c.types; 30 31 32 /** 33 * A structure containing a weak reference to a #GObject. 34 * 35 * A `GWeakRef` can either be empty (i.e. point to %NULL), or point to an 36 * object for as long as at least one "strong" reference to that object 37 * exists. Before the object's #GObjectClass.dispose method is called, 38 * every #GWeakRef associated with becomes empty (i.e. points to %NULL). 39 * 40 * Like #GValue, #GWeakRef can be statically allocated, stack- or 41 * heap-allocated, or embedded in larger structures. 42 * 43 * Unlike g_object_weak_ref() and g_object_add_weak_pointer(), this weak 44 * reference is thread-safe: converting a weak pointer to a reference is 45 * atomic with respect to invalidation of weak pointers to destroyed 46 * objects. 47 * 48 * If the object's #GObjectClass.dispose method results in additional 49 * references to the object being held (‘re-referencing’), any #GWeakRefs taken 50 * before it was disposed will continue to point to %NULL. Any #GWeakRefs taken 51 * during disposal and after re-referencing, or after disposal has returned due 52 * to the re-referencing, will continue to point to the object until its refcount 53 * goes back to zero, at which point they too will be invalidated. 54 * 55 * It is invalid to take a #GWeakRef on an object during #GObjectClass.dispose 56 * without first having or creating a strong reference to the object. 57 */ 58 public class WeakRef 59 { 60 /** the main Gtk struct */ 61 protected GWeakRef* gWeakRef; 62 protected bool ownedRef; 63 64 /** Get the main Gtk struct */ 65 public GWeakRef* getWeakRefStruct(bool transferOwnership = false) 66 { 67 if (transferOwnership) 68 ownedRef = false; 69 return gWeakRef; 70 } 71 72 /** the main Gtk struct as a void* */ 73 protected void* getStruct() 74 { 75 return cast(void*)gWeakRef; 76 } 77 78 /** 79 * Sets our main struct and passes it to the parent class. 80 */ 81 public this (GWeakRef* gWeakRef, bool ownedRef = false) 82 { 83 this.gWeakRef = gWeakRef; 84 this.ownedRef = ownedRef; 85 } 86 87 /** */ 88 this(void* object) 89 { 90 g_weak_ref_init(gWeakRef, object); 91 } 92 93 /** 94 */ 95 96 /** 97 * Frees resources associated with a non-statically-allocated #GWeakRef. 98 * After this call, the #GWeakRef is left in an undefined state. 99 * 100 * You should only call this on a #GWeakRef that previously had 101 * g_weak_ref_init() called on it. 102 * 103 * Since: 2.32 104 */ 105 public void clear() 106 { 107 g_weak_ref_clear(gWeakRef); 108 } 109 110 /** 111 * If @weak_ref is not empty, atomically acquire a strong 112 * reference to the object it points to, and return that reference. 113 * 114 * This function is needed because of the potential race between taking 115 * the pointer value and g_object_ref() on it, if the object was losing 116 * its last reference at the same time in a different thread. 117 * 118 * The caller should release the resulting reference in the usual way, 119 * by using g_object_unref(). 120 * 121 * Returns: the object pointed to 122 * by @weak_ref, or %NULL if it was empty 123 * 124 * Since: 2.32 125 */ 126 public ObjectG get() 127 { 128 auto __p = g_weak_ref_get(gWeakRef); 129 130 if(__p is null) 131 { 132 return null; 133 } 134 135 return ObjectG.getDObject!(ObjectG)(cast(GObject*) __p, true); 136 } 137 138 /** 139 * Initialise a non-statically-allocated #GWeakRef. 140 * 141 * This function also calls g_weak_ref_set() with @object on the 142 * freshly-initialised weak reference. 143 * 144 * This function should always be matched with a call to 145 * g_weak_ref_clear(). It is not necessary to use this function for a 146 * #GWeakRef in static storage because it will already be 147 * properly initialised. Just use g_weak_ref_set() directly. 148 * 149 * Params: 150 * object = a #GObject or %NULL 151 * 152 * Since: 2.32 153 */ 154 public void init(ObjectG object) 155 { 156 g_weak_ref_init(gWeakRef, (object is null) ? null : object.getObjectGStruct()); 157 } 158 159 /** 160 * Change the object to which @weak_ref points, or set it to 161 * %NULL. 162 * 163 * You must own a strong reference on @object while calling this 164 * function. 165 * 166 * Params: 167 * object = a #GObject or %NULL 168 * 169 * Since: 2.32 170 */ 171 public void set(ObjectG object) 172 { 173 g_weak_ref_set(gWeakRef, (object is null) ? null : object.getObjectGStruct()); 174 } 175 }